package com.romeo_foxtrot.labs.solr.client.json;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

public class Luke extends JavaScriptObject {

	protected Luke() {
	}

	public final native JsArray<Field> getFields() /*-{
		var pairs = [];
		var i = -1;

		var obj = this["fields"];
		for ( var prop in obj) {
			i++;
			pairs[i] = {
				"name" : prop,
				"value" : obj[prop]
			};
		}

		return pairs;
	}-*/;

	public final native Index getIndex() /*-{
		return this["index"];
	}-*/;

	public final native Info getInfo() /*-{
		return this["info"];
	}-*/;

	public final native ResponseHeader getResponseHeader() /*-{
		return this["responseHeader"];
	}-*/;

	public static class Field extends JavaScriptObject {

		protected Field() {
		}

		public final native String getName() /*-{
			return this["name"];
		}-*/;

		public final native int getDistinct() /*-{
			return this["value"]["distinct"];
		}-*/;

		public final native int getDocs() /*-{
			return this["value"]["docs"];
		}-*/;

		/**
		 * optional
		 * 
		 * @return
		 */
		public final native String getDynamicBase() /*-{
			var result = this["value"]["dynamicBase"];
			if (!result) {
				result = "";
			}
			return result;
		}-*/;

		public final native JsArray<Hits> getHistogram() /*-{
			var hits = [];
			var i = -1;

			var mixture = this["value"]["histogram"];
			var mixtureIndex = 0;
			while (mixtureIndex < mixture.length) {
				i++;
				hits[i] = {
					"name" : mixture[mixtureIndex],
					"value" : mixture[mixtureIndex + 1]
				};

				mixtureIndex += 2;
			}

			return hits;
		}-*/;

		public final native String getIndex() /*-{
			return this["value"]["index"];
		}-*/;

		public final native String getSchema() /*-{
			return this["value"]["schema"];
		}-*/;

		public final native JsArray<Hits> getTopTerms() /*-{
			var hits = [];
			var i = -1;

			var mixture = this["value"]["topTerms"];
			var mixtureIndex = 0;
			while (mixtureIndex < mixture.length) {
				i++;
				hits[i] = {
					"name" : mixture[mixtureIndex],
					"value" : mixture[mixtureIndex + 1]
				};

				mixtureIndex += 2;
			}

			return hits;
		}-*/;

		public final native String getType() /*-{
			return this["value"]["type"];
		}-*/;

	}

	public static class Index extends JavaScriptObject {

		protected Index() {
		}

		public final native boolean isCurrent() /*-{
			return this["current"];
		}-*/;

		public final native String getDirectory() /*-{
			return this["directory"];
		}-*/;

		public final native boolean hasDeletions() /*-{
			return this["hasDeletions"];
		}-*/;

		/**
		 * TODO this may need debug
		 * 
		 * @return
		 */
		public final native String getLastModified() /*-{
			return this["lastModified"];
		}-*/;

		public final native int getMaxDoc() /*-{
			return this["maxDoc"];
		}-*/;

		public final native int getNumDocs() /*-{
			return this["numDocs"];
		}-*/;

		public final native int getNumTerms() /*-{
			return this["numTerms"];
		}-*/;

		public final native boolean isOptimized() /*-{
			return this["optimized"];
		}-*/;

		public final native String getVersion() /*-{
			return this["version"] + "";
		}-*/;
	}

	public static class Info extends JavaScriptObject {

		protected Info() {
		}

		public final native String getNOTE() /*-{
			return this["NOTE"];
		}-*/;

		public final native JsArray<Key> getKey() /*-{
			var pairs = [];
			var i = -1;

			var obj = this["key"];
			for ( var prop in obj) {
				i++;
				pairs[i] = {
					"name" : prop,
					"value" : obj[prop]
				};
			}

			return pairs;
		}-*/;

		public static class Key extends JavaScriptObject {

			protected Key() {
			}

			public final native String getName() /*-{
				return this["name"];
			}-*/;

			public final native String getValue() /*-{
				return this["value"];
			}-*/;
		}
	}

	public static class ResponseHeader extends JavaScriptObject {

		protected ResponseHeader() {
		}

		public final native int getQTime() /*-{
			return this["QTime"];
		}-*/;

		public final native int getStatus() /*-{
			return this["status"];
		}-*/;
	}

}
